package com.java.androidmarket.entity;

import com.alibaba.fastjson.JSONObject;
import com.java.androidmarket.exception.BaseErrorInfo;
import com.java.androidmarket.exception.CommonEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.stereotype.Component;

/**
 * description:
 * author:张腾
 * date:2021-04-22
 */

@Data
@Accessors(chain = true)
@Component
@ApiModel(value = "结果类" ,description = "返回给前端的数据打包成对象")
public class Result {

    @ApiModelProperty(value = "状态码", example = "200")
    //-1 -99 -999
    private String code;

    @ApiModelProperty(value = "提示信息", example = "成功！")
    private String msg;

    @ApiModelProperty(value = "数据", required = false)
    private Object data;

    @ApiModelProperty(value = "数据条数", example = "20", required = false)
    private int total;  //可以为空


    public Result(){
    }

    public Result(BaseErrorInfo baseErrorInfo){
        this.code = baseErrorInfo.getResultCode();
        this.msg = baseErrorInfo.getResultMsg();
    }


    /**
     * 成功
     * @return
     */
    public static Result success(){
        return success(null);
    }

    /**
     * 成功
     * @param data
     * @return
     */
    public static Result success(Object data) {
        Result result = new Result();
        result.setCode(CommonEnum.SUCCESS.getResultCode());
        result.setMsg(CommonEnum.SUCCESS.getResultMsg());
        result.setData(data);

        return result;
    }

    /**
     * 成功
     * @param data
     * @return
     */
    public static Result success(Object data,int total) {
        Result result = new Result();
        result.setCode(CommonEnum.SUCCESS.getResultCode());
        result.setMsg(CommonEnum.SUCCESS.getResultMsg());
        result.setData(data);
        result.setTotal(total);

        return result;
    }

    /**
     * 失败
     */
    public static Result error(BaseErrorInfo errorInfo) {
        Result result = new Result();
        result.setCode(errorInfo.getResultCode());
        result.setMsg(errorInfo.getResultMsg());
        result.setData(null);
        return result;
    }

    /**
     * 失败
     */
    public static Result error(String code, String message) {
        Result result = new Result();
        result.setCode(code);
        result.setMsg(message);
        result.setData(null);
        return result;
    }

    /**
     * 失败
     */
    public static Result error( String message) {
        Result result = new Result();
        result.setCode("-1");
        result.setMsg(message);
        result.setData(null);
        return result;
    }

    /**
     * 失败
     */
    public static Result error(String code,String message,Object data) {
        Result result = new Result();
        result.setCode(code);
        result.setMsg(message);
        result.setData(data);
        return result;
    }

    @Override
    public String toString() {
        return JSONObject.toJSONString(this);
    }
}
